/* Keep record of files changed in a text file
     Change from using Companies table to suppliers table
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TeamTime.Classes;
using StillVoiceCommon;
using TeamTime.Classes;
namespace TeamTime
{
    public partial class CompaniesForm : System.Web.UI.Page //TeamTime.Classes.CompressedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Lookups lookups = new Lookups();
                
                Functions.dropDownListPopulate(this.ddlProvinceIDFilter, lookups.getProvinces(true), "ID", "Description");
                this.GridView1.Attributes.Add("SortExpression", "CompanyName");
                EnableControls();
                
                if (Functions.IsNumeric(Request.QueryString["ID"]))
                {
                    this.txtID.Text = Request.QueryString["ID"];
                    LoadDetailsForm(Convert.ToInt32( this.txtID.Text));
                    this.txtSearchFor.Text = this.CompanyFormControl1.CompanyNameRegistered;
                    popUp1.Show();
                }
                if (Request.QueryString["searchfor"] != null && Request.QueryString["searchfor"] != "")
                {
                    this.txtSearchFor.Text = Request.QueryString["searchfor"];
                }
                else
                    if (Session["Companies_SearchFor"] != null) this.txtSearchFor.Text = Session["Companies_SearchFor"].ToString();
                if (Request.QueryString["searchactivities"] != null && Request.QueryString["searchactivities"].ToLower() == "true")
                {
                    this.chkSearchActivities.Checked = true;
                }
                btnSearch_Click(sender, e);
                this.Form.DefaultFocus = this.txtSearchFor.ClientID;
                this.Form.DefaultButton = this.btnSearch.UniqueID;
            }

        }

        private void EnableControls()
        {
            CompanyFormControl1.EnableControls();
        }

        protected void CompanyFormControl1_SaveClicked(object sender, EventArgs e)
        {
            this.txtSearchFor.Text = this.CompanyFormControl1.CompanyNameRegistered;
            BindGrid();
            
            //LoadDetailsForm(this.txtID.Text);
            //popUp1.Show();
        }
        protected void CompanyFormControl1_LoadDupClicked(object sender, EventArgs e) 
        {
            popUp1.Show();
        }
        protected void CompanyFormControl1_CompanyTypeChanged(object sender, EventArgs e)
        {
            popUp1.Show();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Session["Companies_SearchFor"] = this.txtSearchFor.Text;
            BindGrid();
        }

        private void BindGrid()
        {
            this.GridView1.Columns[7].Visible = this.chkSearchNotes.Checked; //Show/Hide the notes col.
            //this.GridView1.Columns[10].Visible = this.chkSearchActivities.Checked; //Show/Hide the activities col.
            Companies companies = new Companies();
            this.GridView1.DataSource = companies.searchCompanies(this.txtSearchFor.Text, this.chkSearchNotes.Checked, this.ddlType.SelectedValue, GridView1.Attributes["SortExpression"], Convert.ToInt16(this.ddlProvinceIDFilter.SelectedValue), this.chkSearchActivities.Checked); 
            this.GridView1.DataBind();
            
        }

       
        //protected void btnSave_Click(object sender, EventArgs e)
        //{
        //    this.lnkLoadDup.Attributes["DupID"] = "";
        //    this.lnkLoadDup.Visible = false;
        //    Companies companies = new Companies();
        //    int dupID = companies.getDuplicateCompanyID(this.txtCompanyName.Text, this.txtID.Text);
        //    if (dupID != 0)//Duplicate exists
        //    {
        //        this.lnkLoadDup.Attributes["DupID"] = dupID.ToString();
        //        this.lnkLoadDup.Visible = true;
        //    }
        //    else //Save
        //    {
        //        Company company = new Company();
        //        if (this.txtID.Text == "") { company.addNew(); } else { company.load(this.txtID.Text); }
        //        company.Fields["Notes"] = this.txtNotes.Text;
        //        company.Fields["CompanyNameRegistered"] = this.txtCompanyNameRegistered.Text;
        //        company.Fields["CompanyName"] = this.txtCompanyName.Text;
        //        company.Fields["TelephoneNumber"] = this.txtTelephoneNumber.Text;
        //        company.Fields["FaxNumber"] = this.txtFaxNumber.Text;
        //        company.Fields["ContactPerson"] = this.txtContactPerson.Text;
        //        company.Fields["AddressPostal"] = this.txtAddressPostal.Text;
        //        company.Fields["AddressPhysical"] = this.txtAddressPhysical.Text;
        //        company.Fields["EmailAddress"] = this.txtEmailAddress.Text;
        //        company.Fields["Notes"] = this.txtNotes.Text;
        //        company.Fields["VATRegNum"] = this.txtVATRegNum.Text;
        //        company.Fields["ProvinceID"] = this.ddlProvinceID.SelectedValue;
        //        company.Fields["WebsiteAddress"] = this.txtWebsiteAddress.Text;
        //        company.Fields["CityTown"] = this.txtCityTown.Text;
        //        company.Fields["Suburb"] = this.txtSuburb.Text;
        //        company.Fields["AddressPostalCode"] = this.txtAddressPostalCode.Text;
        //        company.Fields["AddressPhysicalCode"] = this.txtAddressPhysicalCode.Text;
        //        company.Fields["Cellphone"] = this.txtCellphone.Text;
        //        company.Fields["CompanyRegNum"] = this.CompanyRegNumControl1.CompanyRegNum;
        //        company.Fields["CompanyTypeID"] = this.CompanyRegNumControl1.CompanyTypeID;
        //        company.UserIDModifiedBy = Convert.ToInt32(Session["UserID"]);
        //        company.updateWithUserIDDate();
        //        this.txtID.Text = company.KeyValue;
                
        //        company = null;
        //        BindGrid();
        //        LoadDetailsForm(this.txtID.Text);
        //    }
        //}

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            this.CompanyFormControl1.setForAdding();
            //this.ddlCompanyID.Enabled = true;
            popUp1.Show();
        }
      
        private void LoadDetailsForm(Int32 ID)
        {
            this.CompanyFormControl1.CompanyID = ID;  //Loads the details form.
            
        }
       

        //Grid Methods
        protected void GridView_Sort(object source, DataGridSortCommandEventArgs e)
        {

            GridView1.Attributes.Add("SortExpression", e.SortExpression);
            BindGrid();
        }
        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string id = e.CommandArgument.ToString();
                LoadDetailsForm(Convert.ToInt32( id));
                popUp1.Show();
            }
            else if (e.CommandName == "Delete")
            {
                Company company = new Company();
                string id = e.CommandArgument.ToString();
                company.load(id);
                company.delete();
                BindGrid();
            }

        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
        }
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;  // refer to the GridView
            int newPageIndex = 0;

            if (-2 == e.NewPageIndex)
            { // when click the "GO" Button
                TextBox txtNewPageIndex = null;
                //GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                GridViewRow pagerRow = theGrid.BottomPagerRow; //


                if (null != pagerRow)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                }

                if (null != txtNewPageIndex)
                {
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                }
            }
            else
            {  // when click the first, last, previous and next Button
                newPageIndex = e.NewPageIndex;
            }

            // check to prevent form the NewPageIndex out of the range
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            // specify the NewPageIndex
            theGrid.PageIndex = newPageIndex;

            // display a new page of data
            // GridView1.EditIndex = -1;
            //GridView1.PageIndex = e.NewPageIndex;
            BindGrid();
            // rebind the control
            // in this case of retrieving the data using the xxxDataSoucr control,
            // just do nothing, because the asp.net engine binds the data automatically
        }


  

    }
   

}



